<?php

	/**
	 * <version greek="beta" major="1" minor="0">Cochin Splash Hen</version>
	 * <author email="njones@art-k-tec.com">Nika Jones</author>
	 * <copyright start="2007">Art-k-tec Systems, LLC</copyright>
	 * <license type="Apache2.0" href="http://www.opensource.org/licenses/apache2.0.php">Apache License, Version 2.0</license>
	 */
	
	abstract class bBitwise {
		const NULL ="--";
		
		private static $nMajor =1;
		private static $nMinor =0;
		private static $sRevision ="0000";
			
		public $aBitCheck;
		protected static $aBitwise;
		
		public static function version() { return self::$nMajor.'.'.self::$nMinor.'.'.self::$sRevision; }
		
		/// <summary>
		/// 	Reduces all of the passed in values to one
		/// 	value by or-ing them together (so they should be  bit values)
		/// <summary>
		/// <param type="integer|array" name="...">Can be an integer or an array of integers</param>
		/// <returns type="integer" />
		public static function Reduce() {
			$nReturn = 0;
			$a =func_get_args();

			foreach($a as $b) {
				
				if(in_array($b, self::BitwiseArray())) {
					
					$nReturn =$nReturn | $b;
				} elseif(is_array($b)) {
					
					$nReturn =call_user_func_array(array('self', __FUNCTION__), $b);
				}
			}
			
			return $nReturn;
		}
		
		/// <summary>
		/// 	Allows for you to ad hoc add bitwise properties with an attached value
		/// 	This is good for un organized argument lists. Also this allows for you
		/// 	to manage more than 32 bitwise values.
		/// <summary>
		/// <param type="bBitwise" name="oCallee">The calling object because of a limitation in PHP</param>
		/// <param type="string" name="sProperty">The name of the bitwise property</param>
		/// <param type="mixed" name="mValue" optional="true">The value attached to the property</param>
		/// <returns type="integer" />
		public static function _Argument($oCallee, $sProperty, $mValue =bBitwise::NULL) {
			// First get the constants
			
			if(!isset($oCallee->aBitCheck[0])) {
				self::BitwiseArray();print_r(self::$aBitwise);
				
				$sClass =get_class($oCallee);
				$oReflectionClass =new ReflectionClass($sClass);
				$aConstants = $oReflectionClass->getConstants();
				
				$oCallee->aBitCheck =array();

				$n =0;
				foreach($aConstants as $sConstant=>$mValue) {
					$oCallee->aBitCheck[0][$sConstant] =array('bit'=>self::$aBitwise[$n++], 'value'=>$mValue);
				}
			}
			
				foreach($oCallee->aBitCheck as $sKey=>$aBit) {
					$bHasProperty =isset($aBit[$sProperty]);
					
					if($mValue !==bBitwise::NULL) {
						
						if(($nCount =count($aBit)) <= 31 && !$bHasProperty) {
					
							$oCallee->aBitCheck[$sKey][$sProperty] = array('bit'=>self::$aBitwise[$nCount], 'value'=>$mValue);
							return true;
						}
					} else {
						
						if($bHasProperty) {
							return $aBit[$sProperty]['value'];
						}
					}
				}
				
				if($mValue !== self::NULL) { // then this means that we are on a new line.
					
					$oCallee->aBitCheck[][$sProperty] =array('bit'=>self::$aBitwise[0], 'value'=>$mValue);
					return true;
				}
		}
		
		/// <summary>
		/// 	sets up the internal list of 32 bitwise values
		/// <summary>
		private static function BitwiseArray() {
			
			if(self::$aBitwise ==null) {
				
				$a[0] =0;
				$a[1] =1;
				for($i=2; $i<32; $i++) {
				
					$a[$i] =($a[$i-1] * 2);
				}
			
				self::$aBitwise =$a;
			}
		
			return self::$aBitwise;
		}
	}
?>
<?php return; ?>
<bResourceManager version="1.0">
	<summary 
		name="bBitwise"
		version="1.0" 
		checksum="none" 
		signed="http://www.art-k-tec.com/libs/signatures/bBitwise"
		copyright="2007 art-k-tec systems, llc"
	>
		First resource management package. It's intended to be generic so it can be used in a wide
		variety of PHP projects.
	</summary>
</bResourceManager>